package com.ruoyi.jacfile.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.ruoyi.jacfile.domain.OrderModel;
import com.ruoyi.jacfile.domain.Order_test;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class ITextUtil {

    public static String pdfProduce(List<Order_test> order_test, List<OrderModel> tab1, List<OrderModel> tab2){
        String s = JSON.toJSONString(order_test.get(0));
        JSONObject jsonObject = JSONObject.parseObject(s);
        Map<String,Object> map = (Map<String,Object>)jsonObject;
        String PDF_PATH = "F:\\";
        String filename = "_" + UUID.randomUUID().toString()  + ".pdf";
        String fullFileName = PDF_PATH + filename;

        Rectangle rect = new Rectangle(842.0F, 595.0F);//设置页面大小,在源码中有PageSize.A4  = new Rectangle(595.0F, 842.0F)
//        Document document = new Document(rect, 80, 80, 90, 25); //设置成A4纸大小,内容距左右上下的距离
        Document document = new Document(rect, 10, 10, 10, 10); //设置成A4纸大小,内容距左右上下的距离

        //String fullFileName = PDF_PATH + fileName+".pdf"; //文件全路径
        try {
            PdfWriter.getInstance(document, new FileOutputStream(fullFileName));
            // 插入一个段落
            //设置中文字体
//            BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            BaseFont baseFontChinese = BaseFont.createFont("C:\\Windows\\Fonts\\simkai.ttf", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
            //1.标题字体
            Font headFont =  new Font(baseFontChinese , 20 , Font.BOLD);;
            //2.正文字体
            Font contentFontSecond =  new  Font(baseFontChinese , 12 , Font.NORMAL);

            //设置页脚
            HeaderFooter footer=new HeaderFooter(new Phrase("供应商送货人：                   物料交接人：                   专业厂交接人：             ",contentFontSecond),false);
            /**
             * 0是靠左
             * 1是居中
             * 2是居右
             */
//            footer.setBorderColor(Color.black);
            footer.setBorder(0);
//            footer.setAlignment(2);//设置右对齐
            document.setFooter(footer);


            document.open();//打开PDF文件进行编辑
            String codes = "this is a test message";
//
            if(tab1.get(0).getQrc()==1){
                //二维码生成
                BufferedImage encode = QRCodeUtil.encode(codes, null, false);
                Image instance = Image.getInstance(encode, Color.BLACK);
                instance.scaleAbsolute(100,100);
                document.add(instance);
            }

            if(tab1.get(0).getQrc()==1){
                //条形码生成
                BufferedImage barcodes = BarCodeUtil.insertWords(BarCodeUtil.getBarCode(codes), codes);
                Image barcode128 = Image.getInstance(barcodes,Color.BLACK);
                barcode128.setAbsolutePosition(500,500);
                document.add(barcode128);
            }

            //设置标题
            Paragraph head1 = new Paragraph(tab1.get(0).getOrderTitle(), headFont);
            head1.setAlignment(1);//设置段落居中
            head1.setSpacingAfter(50);
            document.add(head1);

            //使用表格布局
            PdfPTable baseInfoTable1 = new PdfPTable(tab1.size()); //设置表格有多少列
            int width[] = new int[tab1.size()];//设置每列宽度比例
            for(int i=0;i<tab1.size();i++){
                width[i]=tab1.get(i).getWidths();
            }
            baseInfoTable1.setWidths(width);
            baseInfoTable1.setWidthPercentage(100);//占页面宽度比例
            //设置表格中单元格的属性,所有单元格都可以使用
            PdfPCell pdfPCell1 = new PdfPCell();//创建一个单元格
            //设置字体水平居中
            pdfPCell1.setHorizontalAlignment(PdfPCell.LEFT);//设置水平对齐
            //设置字体垂直对齐居中
            pdfPCell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            pdfPCell1.setBorder(0);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = null;

            for(OrderModel tab:tab1){
                pdfPCell1.setPhrase(new Phrase(tab.getGridName() , contentFontSecond));
                baseInfoTable1.addCell(pdfPCell1);
                if(tab.getGridKey().equals("planDate") || tab.getGridKey().equals("printDate")){
                    d= new Date((Long)map.get(tab.getGridKey()));
                    String format = sdf.format(d);
                    pdfPCell1.setPhrase(new Phrase(format , contentFontSecond));
                    baseInfoTable1.addCell(pdfPCell1);
                }else{
                    pdfPCell1.setPhrase(new Phrase(map.get(tab.getGridKey()).toString() , contentFontSecond));
                    baseInfoTable1.addCell(pdfPCell1);
                }
            }
            baseInfoTable1.setSpacingAfter(30);//设置间距


            PdfPTable baseInfoTable2 = new PdfPTable(tab2.size());
            int width2[] = new int[tab2.size()];//设置每列宽度比例
            for(int j=0;j<tab2.size();j++){
                width2[j]=tab2.get(j).getWidths();
            }
            baseInfoTable2.setWidths(width2);
            baseInfoTable2.setWidthPercentage(100);//占页面宽度比例
            //设置表格中单元格的属性,所有单元格都可以使用
            PdfPCell pdfPCell2 = new PdfPCell();//创建一个单元格
            //设置字体水平居中
            pdfPCell2.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);//设置水平对齐
            //设置字体垂直对齐居中
            pdfPCell2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
//            pdfPCell1.setBorder(Rectangle.BOTTOM);
            pdfPCell2.setBorderWidth(1f);
            pdfPCell2.setBorderColor(Color.BLACK);

            for(OrderModel tab:tab2){
                pdfPCell2.setPhrase(new Phrase(tab.getGridName() , contentFontSecond));
                baseInfoTable2.addCell(pdfPCell2);
            }

            map.put("orderNumber","1");

            for(OrderModel tab:tab2){
                    pdfPCell2.setPhrase(new Phrase(map.get(tab.getGridKey()).toString() , contentFontSecond));
                    baseInfoTable2.addCell(pdfPCell2);
            }
            document.add(baseInfoTable1);
            document.add(baseInfoTable2);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
        return fullFileName;
    }
}
